﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OPM.Class;
using OPM.Models;
using System.Xml;

namespace OPM.Controllers
{
    public class InformationController : Controller
    {
        //
        // GET: /Information/

        public ActionResult Index()
        {
            var source = WSO.GetKnowledges(U.Token, "", 0, 10);

            return View(source);
        }

        [HttpGet]
        public ActionResult Source(string id)
        {
            //var type = WSO.GetGroups(U.Token);
            var source = WSO.GetKnowledges(U.Token, "", 0, 10);
            var knowlenge = new List<GetKnowledges>();
            var comment = WSO.GetKnowledgeComments(U.Token, id, 0, 10);
            int count_comment = comment.Count();
            var commentDetail = new List<GetKnowledgeComments>();

            foreach (var list in source)
            {
                if (id == list.id)
                {
                    var smile = WSO.GetKnowledgeSmile(U.Token, list.id);
                    

                    var query_pic = (from x in list.list_content_picture_preview
                                     where x.id == list.id
                                     select x.content_picture).FirstOrDefault();

                    var query_detail = (from x in list.list_content_picture_preview
                                        where x.id == list.id
                                        select x.description).FirstOrDefault();

                    knowlenge.Add(new GetKnowledges
                    {
                        id = list.id,
                        seq = list.seq,
                        title_th = list.title_th,
                        title_en = list.title_en,
                        date_start = list.date_start,
                        date_expire = list.date_expire,
                        count_smile = smile.count_smile,
                        picture = query_pic,
                        detail = query_detail,
                        count_comment = count_comment
                    });
                }
            }

            foreach (var r in comment)
            {
                commentDetail.Add(new GetKnowledgeComments
                {
                    id = r.id,
                    ref_id = r.ref_id,
                    line = r.line,
                    content_id = r.content_id,
                    comment_text = r.comment_text,
                    is_enable = r.is_enable,
                    tq_key = r.tq_key,
                    created_by_text = r.created_by_text,
                    updated_by_text = r.updated_by_text,
                    created = r.created,
                    created_by = r.created_by,
                    updated = r.updated_by,
                    updated_by = r.updated_by,

                });
            }

            ViewBag.comment = commentDetail;
            ViewBag.knowlenge = knowlenge;
            return View();
        }

        public ActionResult Detail(string id)
        {
            var detail = WSO.GetKnowledgeDetail(U.Token, id);
            var smile = WSO.GetKnowledgeSmile(U.Token, id);
            var knowlengedetail = new List<GetKnowledgeDetail>();
            var comment = WSO.GetKnowledgeComments(U.Token, id, 0, 1000);
            var commentDetail = new List<GetKnowledgeComments>();

            var query_pic = (from x in detail.list_content_picture_preview
                             where x.id == detail.id
                             select x.content_picture).FirstOrDefault();

            var query_id = (from x in detail.list_content_picture_preview
                            where x.id == detail.id
                            select x.id).FirstOrDefault();

            knowlengedetail.Add(new GetKnowledgeDetail
            {
                id = detail.id,
                seq = detail.seq,
                type_id = detail.type_id,
                group_id = detail.group_id,
                title_th = detail.title_th,
                title_en = detail.title_en,
                detail_th = detail.detail_th,
                date_start = detail.date_start,
                date_expire = detail.date_expire,
                remark = detail.remark,
                is_public = detail.is_public,
                smile = smile.count_smile,
                picture = query_pic,
                contend_id = query_id,
                ShowDateTime = detail.ShowDateTime

            });

            foreach (var r in comment)
            {
                commentDetail.Add(new GetKnowledgeComments
                {
                    id = r.id,
                    ref_id = r.ref_id,
                    line = r.line,
                    content_id = r.content_id,
                    comment_text = r.comment_text,
                    is_enable = r.is_enable,
                    tq_key = r.tq_key,
                    created_by_text = r.created_by_text,
                    updated_by_text = r.updated_by_text,
                    created = r.created,
                    created_by = r.created_by,
                    updated = r.updated_by,
                    updated_by = r.updated_by,
                    boxId = "BoxId_" + r.id,
                    ShowDateTime = r.ShowDateTime
                });
            }

            ViewBag.knowlengedetail = knowlengedetail;
            ViewBag.commentDetail = commentDetail;

            return View();
        }

        [HttpGet]
        public JsonResult AddKnowledgeComment(string content_id, string comment, string ref_id)
        {
            string stringSeparators = "\n";
            string text = comment;
            string lines = text.Replace(stringSeparators, "");
            lines = lines.Replace("\\n", "\n");

            //var commentDetail = WSO.AddKnowledgeComment(U.Token, content_id, comment, ref_id);

            var commentDetail = WSO.AddKnowledgeComment(U.Token, content_id, lines, "");

            return Json(commentDetail, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult EditComment(string id, string content_id, string comment, string ref_id)
        {
            var editComment = WSO.EditKnowledgeComment(U.Token, id, content_id, comment, ref_id);
           // var editComment1 = WSO.EditKnowledgeComment(U.Token, id, "", comment, "");
           // var editComment2 = WSO.EditKnowledgeComment(U.Token, id, "", comment, ref_id);

           return Json("", JsonRequestBehavior.AllowGet);
        }
    }
}
